//
//  BadgeButton.m
//  SinaWebo
//
//  Created by 亦心 on 15/3/8.
//  Copyright (c) 2015年 亦心. All rights reserved.
//

#import "BadgeButton.h"
#import "UIImage+Extension.h"
#import "NSString+Extension.h"

#define KDistance 60.0

@interface BadgeButton ()
@property (weak, nonatomic) UIView *smalView;
@property (assign, nonatomic) CGFloat smalRadius;
@property (weak, nonatomic) CAShapeLayer *shapeLayer;
@property (copy, nonatomic) FinishBlock comBlock;
@end

@implementation BadgeButton

- (UIView *)smalView{
    if (_smalView == nil) {
        UIView *smalView = [[UIView alloc] init];
        smalView.backgroundColor =  MyColor(253, 95, 60);
        [self.superview insertSubview:smalView belowSubview:self];
        _smalView = smalView;
    }
    return _smalView;
}

- (CAShapeLayer *)shapeLayer{
    if (_shapeLayer == nil) {
        CAShapeLayer *shapeLayer = [[CAShapeLayer alloc] init];
        shapeLayer.fillColor = MyColor(253, 95, 60).CGColor;
        [self.superview.layer insertSublayer:shapeLayer below:self.layer];
        _shapeLayer = shapeLayer;
    }
    return _shapeLayer;
}

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self initBadgeButton];
    }
    return self;
}

- (instancetype)initWithCoder:(NSCoder *)coder{
    self = [super initWithCoder:coder];
    if (self) {
        [self initBadgeButton];
    }
    return self;
}

- (void)badgeValue:(NSString *)badgeValue finishBlock:(FinishBlock)block{
    _comBlock = block;
    self.badgeValue = badgeValue;
}

-(void)setBadgeValue:(NSString *)badgeValue{
    _badgeValue=[badgeValue copy];
    
    //设置提示数字
    if (badgeValue) {
        self.hidden=NO;
        [self setTitle:badgeValue forState:UIControlStateNormal];
        self.layer.cornerRadius = self.height * 0.5;
        self.smalView.center = self.center;
        self.smalView.bounds = CGRectMake(0, 0, self.height * 0.7, self.height * 0.7);
        self.smalView.layer.cornerRadius = self.smalView.height * 0.5;
        self.smalRadius = self.smalView.width * 0.5;
        if (badgeValue.integerValue > 9) {
            self.width +=5;
        }
    }else{
        self.hidden=YES;
    }
}

-(void)initBadgeButton{
    self.hidden=YES;
    self.backgroundColor = MyColor(253, 95, 60);
    self.titleLabel.font=[UIFont systemFontOfSize:11];
    UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(onPanGesture:)];
    [self addGestureRecognizer:panGesture];
}

- (void)onPanGesture:(UIPanGestureRecognizer *)panGesture{
    CGPoint point = [panGesture locationInView: self.superview];
    self.centerX = point.x;
    self.centerY = point.y;
    
    [panGesture setTranslation:CGPointZero inView:self];
    
    CGFloat distance = [self sumCentDistanc:self.smalView.center bigPoint:self.center];
    CGFloat smalRadius = self.smalRadius - distance / 10;
    
    self.smalView.bounds = CGRectMake(0, 0, smalRadius * 2, smalRadius * 2);
    self.smalView.layer.cornerRadius = smalRadius;
    
    if (distance > KDistance) {
        [self deleteShapeLayer];
    }else if (distance > 0 && distance < KDistance) {
        self.smalView.hidden = NO;
        self.shapeLayer.path = [self path:self.smalView bigView:self].CGPath;
    }
    
    
    if (panGesture.state == UIGestureRecognizerStateEnded) {
        if (distance > KDistance) {
//            [self startAnimation];
            if (_block) {
                _block();
            }
            if (_comBlock) {
                _comBlock();
            }
            
            [self removeFromSuperview];
            
        }else{
            [self deleteShapeLayer];
            [UIView animateWithDuration:0.3 delay:0.0 usingSpringWithDamping:0.2 initialSpringVelocity:0.0 options:UIViewAnimationOptionCurveLinear animations:^{
                self.center = self.smalView.center;
            } completion:^(BOOL finished) {
//                self.smalView.hidden = NO;
            }];
        }
    }
    
}

- (void)startAnimation{
    UIImageView *gifView = [[UIImageView alloc] init];
    gifView.backgroundColor = [UIColor clearColor];
    gifView.frame = self.bounds;
    [self addSubview:gifView];
    NSMutableArray *gifArray = [NSMutableArray array];
    for (int index = 1; index < 9; index++) {
        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"%d.jpg", index]];
        [gifArray addObject:image];
    }
    gifView.animationImages = gifArray;
    gifView.animationRepeatCount = 1;
    gifView.animationDuration = 0.5;
    [gifView startAnimating];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.4 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self removeFromSuperview];
    });
}

- (void)deleteShapeLayer{
    self.smalView.hidden = YES;
    [self.shapeLayer removeFromSuperlayer];
    self.shapeLayer = nil;
}

- (CGFloat)sumCentDistanc:(CGPoint)smallPoint bigPoint:(CGPoint)bigPoint{
    CGFloat offsetX = smallPoint.x - bigPoint.x;
    CGFloat offsetY = smallPoint.y - bigPoint.y;
    return sqrt(offsetX * offsetX + offsetY * offsetY);
}

- (UIBezierPath *)path:(UIView *)sView bigView:(UIView *)bView{
    CGPoint bCenter = bView.center;
    CGFloat X2 = bCenter.x;
    CGFloat Y2 = bCenter.y;
    CGFloat R2 = bView.width * 0.5 -1.8;
    
    CGPoint sCenter = sView.center;
    CGFloat X1 = sCenter.x;
    CGFloat Y1 = sCenter.y;
    CGFloat R1 = sView.width * 0.5;
    
    CGFloat d2 = [self sumCentDistanc:sView.center bigPoint:bCenter];
    MyLog(@"%f", d2);
    CGFloat d1 = d2 * 0.5;
    CGFloat cosΘ = (Y2 - Y1) / d2;
    CGFloat sinΘ = (X2 - X1) / d2;
    
    CGPoint A = CGPointMake(X1 -  R1 * cosΘ, Y1 + R1 * sinΘ);
    CGPoint B = CGPointMake(X1 + R1 * cosΘ, Y1 - R1 * sinΘ);
    CGPoint C = CGPointMake(X2 + R2 * cosΘ, Y2 - R2 * sinΘ);
    CGPoint D = CGPointMake(X2 - R2 * cosΘ, Y2 + R2 * sinΘ);
    CGPoint O = CGPointMake(A.x + d1 * sinΘ, A.y + d1 * cosΘ);
    CGPoint P = CGPointMake(B.x + d1 * sinΘ, B.y + d1 * cosΘ);
    
    UIBezierPath *path = [UIBezierPath bezierPath];
    [path moveToPoint:A];
    [path addLineToPoint:B];
    [path addQuadCurveToPoint:C controlPoint:P];
    [path addLineToPoint:D];
    [path addQuadCurveToPoint:A controlPoint:O];
    
    return path;
}
@end
